//获取应用实例
var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');
var app = getApp();
const strAppCode = config.SmallApp;
var page = 1;
var pagesize = 5;
var OrderType = 0;
var reqgetOrderSign = "";
var showlist = [];
var haspay = [];
var nopay = [];
var imgpath = config.imgHost
Page({
  data: {
    // 页面配置 
    winWidth: 0,
    winHeight: 0,
    // tab切换
    currentTab: 0,
    imgpath: imgpath
  },
  onLoad: function () {
    console.log(app.globalData.cinemapojo.UserID)
    var that = this;
    console.log(app.globalData.loginuserinfo.mobile)
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 90;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });

    var getOrderSign = "strAppCode=" + strAppCode + "UserID=" + app.globalData.cinemapojo.UserID + "" + "PayMobile=" + app.globalData.loginuserinfo.mobile + "OrderSource=" + "StartDate=" + "EndDate=" + "OrderType=" + OrderType;
    return req.GetSignStr(getOrderSign).then(res => {
      reqgetOrderSign = res.data.data;
    }).then(() => {
      return req.getOrderList(strAppCode, app.globalData.cinemapojo.UserID, app.globalData.loginuserinfo.mobile, "", "", OrderType, reqgetOrderSign, app.globalData.token).then(res => {
        //  TODO 请求所有订单
        console.log(res)
        showlist = res.data.data;
        // showlist.seatlist = showlist.SeatText
        console.log(showlist)
        for (var i = 0; i < showlist.length; i++) {
          var haschooseseat = [];
          for (var j = 0; j < showlist[i].SeatText.split(",").length; j++) {

            var tempseat = showlist[i].SeatText.split(",")[j].replace("-", "排") + "座"

            haschooseseat.push(tempseat);

          }
          showlist[i].haschooseseat = haschooseseat;
        }
        that.setData({ showlist, winHeight: (318 * showlist.length) + 10 })
      })
    })

    // var getOrderSign = "strAppCode=" + strAppCode + "page=" + page + "pagesize=" + pagesize + "OrderSource=" + "StartDate=" + "EndDate=" + "OrderType=" + OrderType;
    // return req.GetSignStr(getOrderSign).then(res=>{
    //   reqgetOrderSign =res.data.data;
    // }).then(()=>{
    //   return req.GetAllOrderList(strAppCode, page, pagesize, "", "", "", OrderType,reqgetOrderSign,app.globalData.token).then(res=>{
    //     //  TODO 请求所有订单
    //     console.log(res)
    //     showlist = res.data.Result;
    //     console.log(showlist)
    //     that.setData({showlist})
    //   })
    // })

  },

  // 滑动切换tab
  swiperTab: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
    OrderType = e.detail.current;
    if (OrderType === 0 ) {
      console.log(showlist)
 
      that.setData({ showlist, winHeight: (318 * showlist.length) + 10 });
    } else if (OrderType == 1 && haspay.length == 0) {
      var getOrderSign = "strAppCode=" + strAppCode + "UserID=" + app.globalData.cinemapojo.UserID + "" + "PayMobile=" + app.globalData.loginuserinfo.mobile + "OrderSource=" + "StartDate=" + "EndDate=" + "OrderType=" + OrderType;
      return req.GetSignStr(getOrderSign).then(res => {
        reqgetOrderSign = res.data.data;
      }).then(() => {
        return req.getOrderList(strAppCode, app.globalData.cinemapojo.UserID, app.globalData.loginuserinfo.mobile, "", "", OrderType, reqgetOrderSign, app.globalData.token).then(res => {
          //  TODO 请求所有订单
          haspay = res.data.data;
          // showlist.seatlist = showlist.SeatText
          console.log(haspay)
          for (var i = 0; i < haspay.length; i++) {
            var haschooseseat = [];
            for (var j = 0; j < haspay[i].SeatText.split(",").length; j++) {

              var tempseat = haspay[i].SeatText.split(",")[j].replace("-", "排") + "座"

              haschooseseat.push(tempseat);

            }
            haspay[i].haschooseseat = haschooseseat;
          }
          that.setData({ haspay, winHeight: (318 * haspay.length) + 10 })
     
        })
      })
    } else if (OrderType == 2 && nopay.length == 0) {
      var getOrderSign = "strAppCode=" + strAppCode + "UserID=" + app.globalData.cinemapojo.UserID + "" + "PayMobile=" + app.globalData.loginuserinfo.mobile + "OrderSource=" + "StartDate=" + "EndDate=" + "OrderType=" + OrderType;
      return req.GetSignStr(getOrderSign).then(res => {
        reqgetOrderSign = res.data.data;
      }).then(() => {
        return req.getOrderList(strAppCode, app.globalData.cinemapojo.UserID, app.globalData.loginuserinfo.mobile, "", "", OrderType, reqgetOrderSign, app.globalData.token).then(res => {
          //  TODO 请求所有订单
          nopay =res.data.data
          for (var i = 0; i < nopay.length; i++) {
            var haschooseseat = [];
            for (var j = 0; j < nopay[i].SeatText.split(",").length; j++) {

              var tempseat = nopay[i].SeatText.split(",")[j].replace("-", "排") + "座"

              haschooseseat.push(tempseat);

            }
            nopay[i].haschooseseat = haschooseseat;
          }
          that.setData({ nopay, winHeight: (318 * nopay.length) + 10 })
        })
      })
    }
  },
  onShow: function () {
    OrderType = 0;
    nopay = [];
    haspay = [];
  },
  // 点击tab切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  goback: function () {
    wx.showModal({
      title: '提示',
      content: '要返回会员中心么',
      success: function (res) {
        if (res.confirm) {
          wx.switchTab({
            url: '../member/index',
          })
        }
      }
    })
  },
  //未支付订单可以支付
  paynow: function (e) {
    var pojo = e.currentTarget.dataset.pojo;
    console.log(pojo)
    var PlayStartDate = pojo.PlayStartDate.replace(/-/g, '/');
    console.log(PlayStartDate)
    var btime = pojo.PlayStartDate + " " + pojo.PlayStartTime
    var oldTime = (new Date(btime)).getTime();
    console.log(oldTime) //上映时间
    var curtime = new Date().getTime();
    console.log(curtime)  //当前时间
    if (oldTime - curtime < 1800000 && curtime < oldTime) {
      //临近上映时间
      wx.showModal({
        title: '提示',
        content: '上映前半小时不能购票',
      })
    }
    else if (curtime > oldTime) {
      wx.showModal({
        title: '提示',
        content: '已经过了上映时间',
      })
    } else {
      //TODO跳转到详情
      wx.navigateTo({
        url: '../memberpay/index?pojo=' + JSON.stringify(pojo) + '',
      })
    }
  },
  showdetail:function(e){
    var bean = e.currentTarget.dataset.pojo;
    console.log(bean)
    if (bean.StateOrder != 3 && bean.StateOrder != 4 &&  bean.StateOrder != 5){
      wx.showModal({
        title: '提示',
        content: '还未出票,请稍候再试',
      })
    }else{
      wx.navigateTo({
        url: '../showdetail/showdetail?pojo='+JSON.stringify(bean)+'',
      })
    }
      
  }
})
